package com.bupt.controller;


import com.bupt.bean.Device;
import com.bupt.bean.DeviceNetwork;
import com.bupt.bean.DeviceType;
import com.bupt.config.*;
import com.bupt.dao.DeviceNetworkRepository;
import com.bupt.dao.DeviceRepository;
import com.bupt.dao.DeviceTypeRepository;
import com.bupt.service.DeviceService;
import com.bupt.service.PageService;
import com.bupt.vo.ChannelAttributeVo;
import com.bupt.vo.DeviceChannelVo;
import com.bupt.vo.DeviceNetworkVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(value="设备信息接口",description="设备信息的增删改查、设备类型信息、查询当前设备状态、通过ID单个查询、通过设备ID查询设备的基本属性和光源属性、通过设备ID查询设备下的通道信息、通过通道ID查询通道下的属性、通过设备ID查询设备下的网络配置、通过IP修改单个设备下的网络配置、设备类型ID=2时设备光源属性配置、通过通道ID修改开关状态")
@RestController
public class DeviceController {

    @Autowired
    private PageService pageService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceTypeRepository deviceTypeRepository;

    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private DeviceNetworkRepository deviceNetworkRepository;

    @ApiOperation("分页查询设备页面列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "path", dataType = "int"), @ApiImplicitParam(name = "size", value = "每页记录数", required = true, paramType = "path", dataType = "int")})
    @GetMapping("/devicePage/{page}/{size}")
    public QueryResponseResult eventPage(@PathVariable("page") int page, @PathVariable("size") int size, QueryPageRequest queryPageRequest) {
        return pageService.findDeviceList(page, size, queryPageRequest);
    }


    @ApiOperation("设备添加页面")
    /*@ApiImplicitParams({@ApiImplicitParam(name="deviceName",value="设备名称",required=true,paramType="body",dataType="string"),@ApiImplicitParam(name="deviceTypeId",value="设备类型ID",required=true,paramType="body",dataType="int"),
            @ApiImplicitParam(name="longitude",value="经度",required=true,paramType="body",dataType="string"),
            @ApiImplicitParam(name="latitude",value="纬度",required=true,paramType="body",dataType="string")})*/
    @PostMapping("/device/add")
    public DeviceResult add(@RequestParam("deviceName") String deviceName, @RequestParam("deviceTypeId") Integer deviceType, @RequestParam("longitude") String longitude, @RequestParam("latitude") String latitude) {
        return deviceService.add(deviceName, deviceType, longitude, latitude);
    }


    @ApiOperation("设备类型信息")
    @RequestMapping(value = "/deviceType", method = RequestMethod.GET)
    public List<DeviceType> deviceType() {
        ArrayList<DeviceType> list = new ArrayList<DeviceType>();
        return deviceTypeRepository.findAll();
    }

    @ApiOperation("通过IP查询单个设备信息")
    @GetMapping("/device/get/{deviceId}")
    public Device findDeviceById(@PathVariable("deviceId") Integer deviceId) {
        return deviceRepository.findByDeviceId(deviceId);
    }

    @ApiOperation("通过ID修改单个设备信息")
    @PutMapping("/device/edit/{deviceId}")
    public DeviceResult editDeviceById(@PathVariable("deviceId") Integer deviceId, @RequestBody Device device) {
        return deviceService.editDeviceById(deviceId, device);
    }


    @ApiOperation("查询当前设备状态")
    @GetMapping("/device/deviceStatus")
    public Map deviceStatus() {
        return deviceService.deviceStatus();

    }

    @ApiOperation("删除设备")
    @DeleteMapping("/device/deleteById/{deviceId}")
    public Result deleteById(@PathVariable("deviceId") Integer deviceId) {
        return deviceService.deleteById(deviceId);
    }

    @ApiOperation("通过设备ID查询设备的基本属性和光源属性")
    @GetMapping("/device/deviceAttributeById/{deviceId}")
    public Map<String, Object> deviceAttributeById(@PathVariable("deviceId") Integer deviceId) {
        return deviceService.deviceAttributeById(deviceId);
    }



    @ApiOperation("设备类型ID=2时设备光源属性配置")
    @PutMapping("/device/deviceLightAttribute/{deviceTypeId}/{deviceAttributeId}/{deviceAttributeValue}/{deviceAttributeSwitch}")
    public Result deviceLightAttribute(@PathVariable("deviceTypeId") Integer deviceTypeId,@PathVariable("deviceAttributeId") Integer deviceAttributeId, @PathVariable("deviceAttributeValue") String deviceAttributeValue,@PathVariable("deviceAttributeSwitch") Integer deviceAttributeSwitch) {
        return deviceService.deviceLightAttribute(deviceTypeId,deviceAttributeId, deviceAttributeValue,deviceAttributeSwitch);
    }


    @ApiOperation("通过设备ID查询设备下的通道信息")
    @GetMapping("/device/deviceChannelById/{deviceId}")
    public List<DeviceChannelVo> deviceChannelById(@PathVariable("deviceId") Integer deviceId) {
        return deviceService.deviceChannelById(deviceId);

    }


    @ApiOperation("通过通道ID查询通道下的属性")
    @GetMapping("/device/channel/channelAttributeById/{deviceChannelId}")
    public List<ChannelAttributeVo> channelAttributeById(@PathVariable("deviceChannelId") Integer deviceChannelId) {
        return deviceService.channelAttributeById(deviceChannelId);

    }

    @ApiOperation("通过通道ID修改开关状态")
    @PutMapping("/device/channel/editChannelEnable/{deviceChannelId}/{channelEnable}")
    public Result editChannelEnable(@PathVariable("deviceChannelId") Integer deviceChannelId, @PathVariable("channelEnable") Integer channelEnable) {
        return deviceService.editChannelEnable(deviceChannelId, channelEnable);
    }


     @ApiOperation("通道属性配置接口")
    @PutMapping("/device/channelAttribute/{channelAttributeId}/{channelAttributeValue}")
    public Result deviceChannelAttributeUpdate(@PathVariable("channelAttributeId") Integer channelAttributeId, @PathVariable("channelAttributeValue") String channelAttributeValue) {
        return deviceService.deviceChannelAttributeUpdate(channelAttributeId,channelAttributeValue);
    }



    @ApiOperation("通过设备ID查询设备下的网络配置")
    @GetMapping("/device/deviceNetworkById/{deviceId}")
    public Map deviceNetworkById(@PathVariable("deviceId") Integer deviceId) {
        return deviceService.deviceNetworkById(deviceId);

    }

    @ApiOperation("通过IP修改单个设备下的网络配置")
    @PutMapping("/device/deviceNetworkedit/{deviceNetworkId}")
    public DeviceNetworkResult editDeviceNetworkById(@PathVariable("deviceNetworkId") Integer deviceNetworkId, @RequestBody DeviceNetwork deviceNetwork) {
        return deviceService.editDeviceNetworkById(deviceNetworkId, deviceNetwork);
    }
}
